//
//  CertificateDetailViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/13.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "CertificateDetailViewController.h"
#import "CertificateDetailTopTableViewCell.h"
#import "CertificateDetailBottomTableViewCell.h"
#import "QuiteApplyView.h"
#define bottomWhiteViewH 50/WIDTH_6_SCALE
@interface CertificateDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)NSMutableArray * topCellArr;
@property (nonatomic, strong)NSMutableArray * bottomCellArr;
@property (nonatomic, strong)UILabel * certificateNumLab;//凭证编号
@property (nonatomic, strong)UILabel * projectNameLab;//计划名称
@end

@implementation CertificateDetailViewController(networking)
- (void)getVoucherDetail{
    NSDictionary * dic = @{
                           @"userId":[UserModel sharedInstanced].userID,
                           @"cookie":[UserModel sharedInstanced].cookie,
                           @"id":self.helpid
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"plan/getVoucherDetail" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"1"]) {
            NSMutableDictionary *resultDic = [[NSMutableDictionary alloc]initWithDictionary:responseObject[@"list"][0]];
            for (NSString *key in [resultDic allKeys]) {
                if ([[resultDic objectForKey:key] isEqual:[NSNull null]]) {
                    [resultDic setValue:@"" forKey:key];
                }
            }
            self.topCellArr = @[
                                 @{@"title":@"互助计划", @"content":resultDic[@"helpname"]},
                                 @{@"title":@"最高获捐", @"content":resultDic[@"number"]},
                                 @{@"title":@"被保障人", @"content":resultDic[@"adstracttwo"]},
                                 @{@"title":@"身份证号", @"content":resultDic[@"card_id"]},
                                 @{@"title":@"与本人关系", @"content":resultDic[@"relation"]},
                                 @{@"title":@"加入日期", @"content":resultDic[@"join_time"]}].mutableCopy;
            self.projectNameLab.text = [NSString stringWithFormat:@"%@", resultDic[@"adstracttwo"]];
            [self.tableView reloadData];
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation CertificateDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"凭证详情";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    [self bottomViews];
    
    [self getVoucherDetail];//网络接口
}
- (void)bottomViews{
    UIView * bottomWhiteView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bottom, SCREEN_WIDTH, bottomWhiteViewH)];
    bottomWhiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomWhiteView];
    for (int i=0; i<3; i++) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH/3)*i, 0, SCREEN_WIDTH/3, bottomWhiteViewH)];
        btn.tag = 6000+i;
        [btn setTitleColor:getColor(greenBGColor) forState:UIControlStateNormal];
        btn.titleLabel.font = DEF_FontSize_12;
        [bottomWhiteView addSubview:btn];
    }
    for (int i=0; i<2; i++) {
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH/3)*(i+1), 0, 1/WIDTH_6_SCALE, bottomWhiteViewH)];
        lineView.backgroundColor = getColor(@"f7f7f7");
        [bottomWhiteView addSubview:lineView];
    }
    UIButton * healthyBtn = (UIButton *)[bottomWhiteView viewWithTag:6000];
    [healthyBtn setTitle:@"健康要求" forState:UIControlStateNormal];
    [healthyBtn addTarget:self action:@selector(healthyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIButton * contractBtn = (UIButton *)[bottomWhiteView viewWithTag:6001];
    [contractBtn setTitle:@"平台合约" forState:UIControlStateNormal];
    [contractBtn addTarget:self action:@selector(contractBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIButton * clauseBtn = (UIButton *)[bottomWhiteView viewWithTag:6002];
    [clauseBtn setTitle:@"基础计划条款" forState:UIControlStateNormal];
    [clauseBtn addTarget:self action:@selector(clauseBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT - bottomWhiteViewH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 42/WIDTH_6_SCALE;
        [_tableView registerClass:[CertificateDetailTopTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CertificateDetailTopTableViewCell class])];
        [_tableView registerClass:[CertificateDetailBottomTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CertificateDetailBottomTableViewCell class])];
        _tableView.backgroundColor = getColor(@"f5f5f5");
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = 42/WIDTH_6_SCALE;
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 92/WIDTH_6_SCALE)];
        _headerView.backgroundColor = [UIColor whiteColor];
        __weak typeof (self)weakSelf = self;
        //上面分割线
        UIView * topLineView = [[UIView alloc] init];
        topLineView.backgroundColor = getColor(@"eeeeee");
        [_headerView addSubview:topLineView];
        //标题
        UILabel * titleLab = [[UILabel alloc] init];
        [self label:titleLab withFont:DEF_FontSize_14 withText:@"电子凭证" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentLeft withView:_headerView withBGColor:[UIColor clearColor]];
        [_headerView addSubview:titleLab];
        //凭证编号
        self.certificateNumLab = [[UILabel alloc] init];
        [self label:self.certificateNumLab withFont:DEF_FontSize_13 withText:@"凭证编号：No.00000001" withTextColor:[UIColor blackColor] withTextAlignment:NSTextAlignmentLeft withView:_headerView withBGColor:[UIColor clearColor]];
        //计划名
        self.projectNameLab = [[UILabel alloc] init];
        [self label:self.projectNameLab withFont:DEF_FontSize_12 withText:@"中青年抗癌计划" withTextColor:getColor(@"9b9b9b") withTextAlignment:NSTextAlignmentLeft withView:_headerView withBGColor:[UIColor clearColor]];
        //上面分割线
        UIView * bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = getColor(@"eeeeee");
        [_headerView addSubview:bottomLineView];
        //masonry
        [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(weakSelf.headerView);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15/WIDTH_6_SCALE);
            make.top.equalTo(topLineView.mas_bottom).mas_offset(13/WIDTH_6_SCALE);
            make.height.mas_offset(17/WIDTH_6_SCALE);
        }];
        [self.certificateNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.height.mas_offset(14/WIDTH_6_SCALE);
            make.top.equalTo(titleLab.mas_bottom).mas_offset(12/WIDTH_6_SCALE);
        }];
        [self.projectNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.height.mas_offset(14/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.certificateNumLab.mas_bottom).mas_offset(10/WIDTH_6_SCALE);
        }];
        [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf.headerView);
            make.height.mas_offset(1/WIDTH_6_SCALE);
        }];
    }return _headerView;
}
- (NSMutableArray *)topCellArr{
    if (!_topCellArr) {
        _topCellArr = @[
//                        @{@"title":@"互助计划", @"content":@"中青年抗癌计划"},
//                        @{@"title":@"最高获捐", @"content":@"30万元"},
//                        @{@"title":@"被保障人", @"content":@"张晓旭"},
//                        @{@"title":@"身份证号", @"content":@"210118568948752258"},
//                        @{@"title":@"与本人关系", @"content":@"其他"},
//                        @{@"title":@"加入日期", @"content":@"2018年07月14日"}].mutableCopy;
        @{@"title":@"互助计划", @"content":@""},
        @{@"title":@"最高获捐", @"content":@""},
        @{@"title":@"被保障人", @"content":@""},
        @{@"title":@"身份证号", @"content":@""},
        @{@"title":@"与本人关系", @"content":@""},
        @{@"title":@"加入日期", @"content":@""}].mutableCopy;
    }return _topCellArr;
}
- (NSMutableArray *)bottomCellArr{
    if (!_bottomCellArr) {
        _bottomCellArr = @[
                           @{@"title":@"申请退出互助"},
                           @{@"title":@"申请互助"},
                           @{@"title":@"进一步了解博帮"},
                           @{@"title":@"为家人加入计划"},
                           @{@"title":@"邀请好友得奖励"}].mutableCopy;
    }return _bottomCellArr;
}
#pragma makr - action
- (void)healthyBtnClick{
    NSLog(@"健康要求");
}
- (void)contractBtnClick{
    NSLog(@"平台合约");
}
- (void)clauseBtnClick{
    NSLog(@"基础计划条款");
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.section == 0) {
        CertificateDetailTopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CertificateDetailTopTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.topCellArr[indexPath.row];
        if (indexPath.row == 1) {
            [cell.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(cell.contentView);
                make.height.mas_offset(1/WIDTH_6_SCALE);
            }];
        }
        if (indexPath.row == self.topCellArr.count - 1) {
            cell.lineView.hidden = YES;
        }
        return cell;
    }else{
        CertificateDetailBottomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CertificateDetailBottomTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.bottomCellArr[indexPath.row];
        if (indexPath.row == self.bottomCellArr.count - 1) {
            cell.lineView.hidden = YES;
        }
        return cell;
    }return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.topCellArr.count;
    }else{
        return self.bottomCellArr.count;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10/WIDTH_6_SCALE)];
    footerView.backgroundColor = getColor(@"f5f5f5");
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10/WIDTH_6_SCALE;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            QuiteApplyView * quiteApplyView = [[QuiteApplyView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
            [quiteApplyView show];
        }
    }
}
@end
